package com.liuf.di.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuf.di.api.ServiceException;
import com.liuf.di.configuration.kettle.KettleInitFactory;
import com.liuf.di.repository.ISlaveRepository;
import com.liuf.di.service.ISlaveService;
import org.pentaho.di.cluster.ClusterSchema;
import org.pentaho.di.cluster.SlaveServer;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: spring-cloud-liuf-master
 * @description: 集群管理业务处理
 * @author: Mr.Liu
 * @create: 2018-09-26 11:00
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Service
public class SlaveServiceImpl implements ISlaveService {

    /**资源库连接*/
    private Repository repository = null;

    private ObjectId objectId = null;

    private KettleInitFactory kettleInitFactory;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ISlaveRepository slaveRepository;

    @Override
    public PageInfo findSlaves(Map<String, Object> params) throws ServiceException {
        PageHelper.startPage((Integer) params.get("pageNum"),(Integer) params.get("pageSize"));
        return new PageInfo(slaveRepository.findSlaves(params));
    }

    @Override
    public String save(Map<String, Object> param) throws ServiceException {
        repository = kettleInitFactory.getRepositoryInit();
        KettleDatabaseRepository kettleDatabaseRepository = new KettleDatabaseRepository();

        try {
            /**获取集群节点信息*/
            List<SlaveServer> slaveServerList = kettleDatabaseRepository.getSlaveServers();
            /**保存*/
            ClusterSchema clusterSchema = new ClusterSchema("slave-name",slaveServerList);
            SlaveServer slaveServer = new SlaveServer();
            slaveServer.setName(param.get("name").toString());
            slaveServer.setHostname(param.get("Hostname").toString());
            slaveServer.setPort(param.get("Port").toString());
            slaveServer.setWebAppName(param.get("WebAppName").toString());
            slaveServer.setUsername(param.get("Username").toString());
            slaveServer.setPassword(param.get("Password").toString());
            slaveServer.setProxyHostname(param.get("ProxyHostname").toString());
            slaveServer.setProxyPort(param.get("ProxyPort").toString());
            slaveServer.setNonProxyHosts(param.get("ProxyHosts").toString());
            slaveServer.setMaster((Boolean) param.get("Master"));
            objectId = kettleDatabaseRepository.insertClusterSlave(clusterSchema,slaveServer);
        } catch (KettleException e) {
            e.printStackTrace();
        }
        return objectId.getId();
    }

    @Override
    public Map<String, Object> getDatabase(long id) throws ServiceException {
        return null;
    }

    @Override
    public boolean detele(String name) throws ServiceException {
        return false;
    }
}
